package com.sls.game.parser.enfu;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import com.sls.game.pojo.enfu.EnfuGoodsInfo;

/**
 * 
 * @Description：恩付商品批量同步响应消息解析
 * @author Wangxuegang
 * @Date：2013-5-22
 * 
 */
public class EnfuGoodsSynResponse {

	private static Logger log = Logger.getLogger(EnfuGoodsSynResponse.class);

	public static List<EnfuGoodsInfo> parseGoodsSyn(String xmlStr) {
		List<EnfuGoodsInfo> list = new ArrayList<EnfuGoodsInfo>();
		InputSource source = null;
		StringReader read = null;
		String resultcode = null;
		String resultdescription = null;
		try {
			// 得到DOM解析器的工厂实例
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			// 从DOM工厂中获得DOM解析器
			DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
			read = new StringReader(xmlStr);
			source = new InputSource(read);
			// 把要解析的xml文档读入DOM解析器
			Document doc = dbBuilder.parse(source);
			Element root = doc.getDocumentElement();
			NodeList childNodes = root.getChildNodes(); // 获得根元素下的子节点
			// 遍历子节点
			for (int i = 0; i < childNodes.getLength(); i++) {
				Node node = childNodes.item(i); // 对每个子节点进行判断
				if ("apiresult".equals(node.getNodeName())) { // 第二层节点
					Element apiresultNode = (Element) node.getChildNodes();
					NodeList apiresultNodes = apiresultNode.getChildNodes();
					for (int j = 0; j < apiresultNodes.getLength(); j++) {
						Node grandsonNode = apiresultNodes.item(j);
						if ("resultcode".equals(grandsonNode.getNodeName())) {
							resultcode = grandsonNode.getTextContent();
						} else if ("resultdescription".equals(grandsonNode
								.getNodeName())) {
							resultdescription = grandsonNode.getTextContent();
						}
					}
				} else if ("productlists".equals(node.getNodeName())) {
					Element productlistsNode = (Element) node.getChildNodes();
					NodeList productlistsNodes = productlistsNode
							.getChildNodes();
					for (int j = 0; j < productlistsNodes.getLength(); j++) {
						Node productlistsChildNode = productlistsNodes.item(j);
						EnfuGoodsInfo info = new EnfuGoodsInfo();
						info.setResultcode(resultcode);
						info.setResultdescription(resultdescription);
						if ("productlist".equals(productlistsChildNode
								.getNodeName())) {
							Element productNode = (Element) productlistsChildNode
									.getChildNodes();
							NodeList productNodes = productNode.getChildNodes();
							for (int k = 0; k < productNodes.getLength(); k++) {
								Node productChildNode = productNodes.item(k);
								if ("productid".equals(productChildNode
										.getNodeName())) {
									String productid = productChildNode
											.getTextContent();
									info.setProductid(productid);
								} else if ("productname"
										.equals(productChildNode.getNodeName())) {
									String productname = productChildNode
											.getTextContent();
									info.setProductname(productname);
								} else if ("parvalue".equals(productChildNode
										.getNodeName())) {
									String parvalue = productChildNode
											.getTextContent();
									info.setParvalue(parvalue);
								} else if ("buyprice".equals(productChildNode
										.getNodeName())) {
									String buyprice = productChildNode
											.getTextContent();
									info.setBuyprice(buyprice);
								} else if ("producttype"
										.equals(productChildNode.getNodeName())) {
									String producttype = productChildNode
											.getTextContent();
									info.setProducttype(producttype);
								} else if ("inventorystatus"
										.equals(productChildNode.getNodeName())) {
									String inventorystatus = productChildNode
											.getTextContent();
									info.setInventorystatus(inventorystatus);
								}
							}

							list.add(info);
						}
					}
				}
			}
		} catch (Exception e) {
			log.error(e);
		}

		return list;
	}

	// public static void main(String[] args){
	// StringBuilder sb = new StringBuilder();
	// sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
	// sb.append("<response>");
	// sb.append("<apiresult>");
	// sb.append("<resultcode>0</resultcode>");
	// sb.append("<resultdescription>查询成功</resultdescription>");
	// sb.append("</apiresult>");
	// sb.append("<productlists>");
	// sb.append("<productlist>");
	// sb.append("<productid>104</productid>");
	// sb.append("<productname>空中网一卡通按元充值</productname>");
	// sb.append("<parvalue>1.00</parvalue>");
	// sb.append("<buyprice>0.94</buyprice>");
	// sb.append("<producttype>直充</producttype>");
	// sb.append("<inventorystatus>true</inventorystatus>");
	// sb.append("</productlist>");
	// sb.append("<productlist>");
	// sb.append("<productid>101</productid>");
	// sb.append("<productname>武神一卡通10元</productname>");
	// sb.append("<parvalue>10.00</parvalue>");
	// sb.append("<buyprice>9.3</buyprice>");
	// sb.append("<producttype>卡密</producttype>");
	// sb.append("<inventorystatus>true</inventorystatus>");
	// sb.append("</productlist>");
	// sb.append("</productlists>");
	// sb.append("</response>");
	//
	// String str = sb.toString().trim();
	// parseGoodsSyn(str);
	// }

}
